package destiny.music;

import java.io.InputStream;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.SAXParserFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import destiny.music.entity.Music;

public class MyToolkit {
	static SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();

	public static Vector<Music> parse(InputStream in) throws Exception {
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
		Vector<Music> musics = new Vector<Music>();
		Document doc = documentBuilder.parse(in);
		Element root = (Element) doc.getChildNodes().item(0);
		NodeList nodeList = root.getChildNodes();
		Element ele;
		for (int i = 0; i < nodeList.getLength(); i++) {
			try{
				ele = (Element) nodeList.item(i);
				Music music = new Music(i, ele.getAttribute("name"), ele.getAttribute("src"), Long.parseLong(ele.getAttribute("size")));
				musics.add(music);
				System.out.println(music);
			}catch(Exception e){
				System.out.println(nodeList.item(i));
			}
		}
		return musics;
	}
}
